package cn.rkylin.oms.system.splitRule.service;

import java.net.URLDecoder;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageInfo;

import cn.rkylin.core.service.ApolloService;
import cn.rkylin.oms.system.shop.service.IShopService;
import cn.rkylin.oms.system.shop.vo.ShopVO;
import cn.rkylin.oms.system.splitRule.dao.ISplitRuleDAO;
import cn.rkylin.oms.system.splitRule.domain.SplitRule;
import cn.rkylin.oms.system.splitRule.vo.SplitRuleVO;

@Service("splitRuleService")
public class SplitRuleServiceImpl extends ApolloService implements ISplitRuleService {
	private static final String RESULT_SUCCESS_FLG = "success";  
	private static final String RESULT_ERROR_MESSAGE = "errormessage";
	private static final String RESULT_ROW_COUNT = "rowcount";
	private static final String RESULT_SPLIT_RULE_ID = "splitruleid";
	
	private Map<String , Object> result;
	/**
     * 店铺服务层
     */
    @Autowired
    private IShopService shopService;
    
	/**
	 * 分单规则数据访问
	 */
	@Autowired
	private ISplitRuleDAO splitRuleDAO;

	/**
	 * 构造函数
	 */
	public SplitRuleServiceImpl(){

	}
	
	public ISplitRuleDAO getSplitRuleDAO() {
		return splitRuleDAO;
	}

	public void setSplitRuleDAO(ISplitRuleDAO splitRuleDAO) {
		this.splitRuleDAO = splitRuleDAO;
	}
	
	
	public Map<String, Object> getResult() {
		return result;
	}

	public void setResult(Map<String, Object> result) {
		this.result = result;
	}

	/**
	 * 查询分单规则
	 * 
	 * @param SplitRuleVO
	 */
	public PageInfo<SplitRuleVO> findByWhere(int page, int rows, SplitRuleVO splitRuleVO) throws Exception {
		PageInfo<SplitRuleVO> SplitRuleVOList = findPage(page, rows, "pageSelectSplitRule", splitRuleVO);
		return SplitRuleVOList;
	}

	/**
	 * 创建分单规则
	 * 
	 * @param SplitRuleVO
	 */
	@Transactional
	@Cacheable(value="splitRule", key="T(String).valueOf('splitRuleShop:').concat(#splitRuleVO.shopId)", unless="#result == null")
	public SplitRule insert(SplitRuleVO splitRuleVO) throws Exception{
		SplitRule splitRule = new SplitRule();
		BeanUtils.copyProperties(splitRuleVO, splitRule);
		splitRuleDAO.insert(splitRule);
		return splitRule;
	}

	/**
	 * 修改分单规则
	 * 
	 * @param SplitRuleVO
	 */
	public Map<String , Object>  update(SplitRuleVO splitRuleVO) throws Exception{
//		SplitRule splitRule = new SplitRule();
//		BeanUtils.copyProperties(splitRuleVO, splitRule);
//		return splitRuleDAO.update(splitRule);
		return null;
	}

	/**
	 * 删除分单规则
	 * 
	 * @param SplitRuleVO
	 */
	public Map<String , Object>  delete(SplitRuleVO splitRuleVO) throws Exception{
//		SplitRule splitRule = new SplitRule();
//		BeanUtils.copyProperties(splitRuleVO, splitRule);
//		return splitRuleDAO.delete(splitRule);
		return null;
	}

	/**
	 * 删除分单规则
	 * 
	 * @param SplitRuleVO
	 */
	public Map<String , Object>  validate(SplitRuleVO splitRuleVO) throws Exception{
//		return 0;
		return null;
	}

	/**
	 * 启用分单规则
	 * 
	 * @param shopId
	 */
	@Transactional
	@CachePut(value="splitRule", key="T(String).valueOf('splitRuleShop:').concat(#splitRule.shopId)", unless="#result == null")
	public SplitRule setEnable(SplitRule splitRule) throws Exception{
		splitRuleDAO.update(splitRule);
		return splitRule;
	}

	/**
	 * 禁用分单规则
	 * 
	 * @param shopId
	 */
	@Transactional
	@CachePut(value="splitRule", key="T(String).valueOf('splitRuleShop:').concat(#splitRule.shopId)", unless="#result == null")
	public SplitRule setDisable(SplitRule splitRule) throws Exception{
		splitRuleDAO.update(splitRule);
		return splitRule;
	}
	
	public Map<String, Object> getSplitRule(String shopId , String shopName , String splitType) throws Exception{
		Map<String, Object> returnMap = new HashMap<String, Object>();
		SplitRuleVO splitRuleVO = new SplitRuleVO();
        
        //接受参数
        splitRuleVO.setSplitRuleId(java.util.UUID.randomUUID().toString().replaceAll("-", ""));
        splitRuleVO.setShopId(URLDecoder.decode(shopId, "UTF-8"));
        splitRuleVO.setShopName(URLDecoder.decode(shopName, "UTF-8"));
        splitRuleVO.setSplitType(URLDecoder.decode(splitType, "UTF-8"));
        splitRuleVO.setCreateTime(new Date());
        splitRuleVO.setUpdateTime(new Date());
        splitRuleVO.setEnable("n");
        splitRuleVO.setDeleted("n");
        
        //check
		if (StringUtils.isBlank(splitRuleVO.getShopId())){
			returnMap.put(RESULT_SUCCESS_FLG,"N");
			returnMap.put(RESULT_ERROR_MESSAGE,"所选择的的店铺已经不存在");
			returnMap.put(RESULT_ROW_COUNT,"0");
        	return returnMap;
		}
		
		ShopVO paramVO = new ShopVO();
        paramVO.setShopId(splitRuleVO.getShopId());
        PageInfo<ShopVO> shopList = shopService.findByWhere(1, 1, paramVO);
        if (shopList.getList()==null || shopList.getList().size() <=0){
        	returnMap.put(RESULT_SUCCESS_FLG,"N");
        	returnMap.put(RESULT_ERROR_MESSAGE,"所选择的的店铺已经不存在");
        	returnMap.put(RESULT_ROW_COUNT,"0");
        	return returnMap;
        }
        
        String shopNm = shopList.getList().get(0).getShopName();
        String shopType = shopList.getList().get(0).getShopType();
        if (!shopNm.equals(splitRuleVO.getShopName())){
        	returnMap.put(RESULT_SUCCESS_FLG,"N");
        	returnMap.put(RESULT_ERROR_MESSAGE,"所选择的的店铺的名字有问题");
        	returnMap.put(RESULT_ROW_COUNT,"0");
        	return returnMap;
        }
        splitRuleVO.setShopType(shopType);
		//唯一check
		SplitRuleVO splitParamVO = new SplitRuleVO();
		splitParamVO.setShopId(splitRuleVO.getShopId());
		PageInfo<SplitRuleVO> SplitRuleVOList = findByWhere(1, 1, splitParamVO);
		if (SplitRuleVOList.getList()!=null && SplitRuleVOList.getList().size() > 0){
			returnMap.put(RESULT_SUCCESS_FLG,"N");
			returnMap.put(RESULT_ERROR_MESSAGE,"要创建的店铺分单规则已经存在");
			returnMap.put(RESULT_ROW_COUNT,"0");
			return returnMap;
		}
		returnMap.put(RESULT_ROW_COUNT,1);
		returnMap.put(RESULT_SUCCESS_FLG,"Y");
		returnMap.put("SplitRuleVO", splitRuleVO);
		
		return returnMap;
	}
}
